package com.lanou.dllo.findfood.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;

import com.lanou.dllo.findfood.R;
import com.lanou.dllo.findfood.entity.BookDesiption;
import com.lanou.dllo.findfood.entity.BookList;
import com.lanou.dllo.findfood.dialog.BookProgressDialog;
import com.lanou.dllo.findfood.util.JazzyViewPager;
import com.lanou.dllo.findfood.volley.VolleySington;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 15/10/28.
 */
public class BookDesptionActivity extends AppCompatActivity {

    private BookViewPerAdpter adpter;
    private  int id;
    private JazzyViewPager mJazzy;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book);
        setupJazziness(JazzyViewPager.TransitionEffect.Tablet);

        getID();
        getBook();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add("Toggle Fade");
        String[] effects = this.getResources().getStringArray(R.array.jazzy_effects);
        for (String effect : effects)
            menu.add(effect);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getTitle().toString().equals("Toggle Fade")) {
            mJazzy.setFadeEnabled(!mJazzy.getFadeEnabled());
        } else {
            JazzyViewPager.TransitionEffect effect = JazzyViewPager.TransitionEffect.valueOf(item.getTitle().toString());
            setupJazziness(effect);
        }
        return true;
    }


    private void setupJazziness(JazzyViewPager.TransitionEffect effect) {
        mJazzy = (JazzyViewPager) findViewById(R.id.jazzy_pager);
        mJazzy.setTransitionEffect(effect);
        adpter = new BookViewPerAdpter(getSupportFragmentManager(),mJazzy);
        mJazzy.setAdapter(adpter);
        mJazzy.setPageMargin(30);
    }


   public void getID() {
        id = getIntent().getIntExtra("id",1370);
    }

    public void getBook() {
        final BookProgressDialog bookProgressDialo = new BookProgressDialog(this,R.id.animtionImg);
        bookProgressDialo.show();
        String httpUrl = "http://apis.baidu.com/tngou/book/show";
        String httpArg = "id=" + id;

        String path = httpUrl + "?" + httpArg;

        StringRequest reqest = new StringRequest(path, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson g = new Gson();
                BookDesiption desiption = g.fromJson(response,BookDesiption.class);
                if (desiption != null){
                    List<BookList> list = desiption.getList();

                    adpter.addData(list);
                    bookProgressDialo.dismiss();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        }) {
            @Override
            //在StringRespset结束之前写{}在大括号里面写 getHeaders() 自动重写方法
            //用Map方法键值对形势添加
            //添加请求头
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put("apikey", "7df89e2ed7c7b92e1a2fe5ec8036d0cd");
                return map;
            }
        };

        VolleySington.getInstance().addRequest(reqest);

    }

}
